package org.itboys.basetemplate.service;

import org.itboys.basetemplate.entity.Voucher;
import org.itboys.basetemplate.entity.VoucherRule;
import org.itboys.basetemplate.entity.VoucherUserMap;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class VoucherUserMapService extends BaseService<VoucherUserMap, Long> {

	private static final long serialVersionUID = -2792207845905508305L;

	@Resource(name = "adminDS")
	private MongoDataSource dictDataSource;

	@Autowired
	private VoucherRuleService voucherRuleService;
	@Autowired
	private VoucherService voucherService;
	
	protected MongoDataSource getMongoDataSource() {
		return dictDataSource;
	}

	protected Class<VoucherUserMap> getEntityClass() {
		return VoucherUserMap.class;
	}

	//支付时选择折扣券列表
	public List<VoucherUserMap> getPayVoucherList(Long memberId, Double amount) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		String now = format.format(new Date());
		Map<String, Object> map = new HashMap<>();
		map.put("memberId", memberId);
		map.put("status", 0);
		List<VoucherUserMap> list = list(map);
		List<VoucherUserMap> vouchers = new ArrayList<>();
		for (VoucherUserMap userMap : list) {
			if (userMap.getEndDate().compareTo(now) >= 0 && userMap.getStartDate().compareTo(now) <= 0) {
				if (userMap.getAmount() <= amount) {
					vouchers.add(userMap);
				}
			}
		}
		return vouchers;
	}
	
	/**
	 * 新用户注册分配折扣券
	 * @param memberId
	 * @return
	 */
	public void giveByRegister(Long memberId, Long centerId) {
		Map<String, Object> map = new HashMap<>();
		map.put("status", 1);
		map.put("centerId", centerId);
		map.put("name", "新用户注册");
		List<VoucherRule> list = voucherRuleService.list(map);
		if(list != null && list.size() > 0) {
			Voucher voucher = voucherService.getById(list.get(0).getVoucherId());
			if(voucher != null) {
				giveVoucher(memberId, voucher);
			}
		}
	}
	
	/**
	 * 是否存在折扣券
	 * 
	 * @param memberId
	 * @param voucherId
	 * @return
	 */
	public VoucherUserMap isExist(Long memberId, Long voucherId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberId", memberId);
		map.put("voucherId", voucherId);
		List<VoucherUserMap> list = this.list(map);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 分配折扣券
	 * @param idLong
	 * @param voucher
	 * @return
	 */
	@Transactional("transactionManager")
	public VoucherUserMap giveVoucher(Long idLong, Voucher voucher) {
		SimpleDateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd");
		VoucherUserMap vMap = new VoucherUserMap();
		vMap.setMemberId(idLong);
		vMap.setStatus(0);
		if (voucher.getType() == 1) {
			vMap.setStartDate(voucher.getStartDate());
			vMap.setEndDate(voucher.getEndDate());
		} else if (voucher.getType() == 2) {
			vMap.setStartDate(sFormat.format(new Date()));
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.add(Calendar.DAY_OF_MONTH, voucher.getDayCount());
			vMap.setEndDate(sFormat.format(calendar.getTime()));
		}
		vMap.setAmount(voucher.getAmount());
		vMap.setMoney(voucher.getMoney());
		vMap.setName(voucher.getName());
		this.save(vMap);
		return vMap;
	}
}
